<?php

class load{
    var $path = array(); //Path in the url (eg. /view/35) or homepage [cfg]
    var $cfg; // from the config
    function  __construct($main_cfg) {
        $this->cfg = $main_cfg;
        $this->path = $this->get_url_segments($_GET['q']);
        $this->base_url = $this->get_base_url();
    }
    public function get_url_segments ($pathname = null){
            $q = $pathname != "/" ? $pathname : $this->cfg['home'];
            
            $temp = explode("/", $q);

            if($temp[count($temp)-1] == ""){ unset ($temp[count($temp)-1]); }
            return $temp;
    }
    public function get_base_url(){
        $base = 'http';
        @$base .= $_SERVER["HTTPS"] == "on" ? "s" : "";
        $base .= "://";
        $base .= $_SERVER["SERVER_NAME"];
        $base .= $_SERVER["SERVER_PORT"] != "80" ? ":".$_SERVER["SERVER_PORT"] : "";
        $base .= substr($_SERVER["PHP_SELF"], 0, -13);

        return $base;
    }
    public function module_by_string($module_name,$method_name){
        $module_path = '../mod/'.$module_name.'/'.$module_name.'.mod.php';

        if(file_exists($module_path)){
                include_once '../mod/'.$module_name.'/'.$module_name.'.mod.php';


                $module_instance = new $module_name();
                
                
                if(isset($method_name) && method_exists($module_instance, $method_name)){
                    $module_instance->$method_name();
                }

                $module_instance->load_layout();
        }else{
            header("Location: ". $this->base_url.$this->cfg["error"]);
        }
    }

    
}

?>
